package cn.adair.oxframe.base;

import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.adair.oxframe.bean.Titlebean;
import cn.adair.oxframe.views.ExampleFragment;

/**
 * fragment 懒加载
 * 这里利用反射 new 新的Fragment
 */
public class OxBaseFragmentAdapter extends FragmentPagerAdapter {

    private Class mClazz;
    private Class[] mClazzs;
    private boolean isOneFragment; // 是否使用同一个Fragment
    private List<Titlebean> mTitles;
    private Map<Integer, Fragment> mFragments;

    public OxBaseFragmentAdapter(@NonNull FragmentManager fm) {
        super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        this.mFragments = new HashMap<>();
    }

    /**
     * Fragment 布局相同时使用
     *
     * @param clazz xxxFragment.class
     */
    public void setFragment(Class clazz) {
        this.mClazz = clazz;
        this.isOneFragment = true;
    }

    /**
     * Fragment 布局不同时使用
     *
     * @param clazzs xxxFragment.class 数组
     */
    public final void setFragments(Class... clazzs) {
        this.mClazzs = clazzs;
        this.isOneFragment = false;
    }

    /**
     * todo 之后可能会增加 activity 通过该对象传递到fragment 一些自定义参数
     * Fragment 标题名称、标题编码
     *
     * @param titles 泛型列表
     */
    public void setFragmentTitles(List<Titlebean> titles) {
        this.mTitles = titles;
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @NonNull
    @Override
    public Fragment getItem(int position) {
        Fragment fragment = mFragments.get(position);
        if (null != fragment) {
            return fragment;
        }
        Class iClass;
        if (isOneFragment) {  //使用同一Fragment
            iClass = this.mClazz;
        } else { // 使用不同Fragment
            if (mClazzs.length != this.mTitles.size()) {
                return new ExampleFragment("Fragment 与 FragmentTitle 数量不符");
            } else {
                iClass = this.mClazzs[position];
            }
        }
        if (OxBaseFragment.class.isAssignableFrom(iClass)) {
            try {
                fragment = (OxBaseFragment) iClass.newInstance();
                ((OxBaseFragment) fragment).setTitlebean(mTitles.get(position));
                mFragments.put(position, fragment);
            } catch (IllegalAccessException | InstantiationException e) {
                fragment = new ExampleFragment("Fragment 类型转换出错");
            }
        } else {
            fragment = new ExampleFragment("Fragment 没有继承OxBaseFragment");
        }
        return fragment;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        Titlebean bean = mTitles.get(position);
        return bean.iTitle();
    }

    @Override
    public int getCount() {
        return mTitles.size();
    }

}
